import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/404'] // no redirect whitelist
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.state.user.token
  // if (token && to.path === '/login' && store.getters.id) {
  //   next('/')
  // } else if (token && !store.getters.id) {
  //   return next('/login')
  //   // NProgress.done()
  // } else {
  //   next()
  // }
  // if (!token && whiteList.includes(to.path)) {
  //   next()
  // } else {
  //   await Message.error('请重新登录')
  //   return next('/login')
  //   // NProgress.done()
  // }
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // if (!store.getters.id) {
      //   // await Message.error('请重新登录')
      //   // store.commit('user/updateUserInfo')
      //   // next('/login')
      //   // NProgress.done()
      // }
      next()
    }
    // await
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      await Message.error('请重新登录')
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => NProgress.done())

